﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for TestDatabaseManager
/// </summary>
public class TestDatabaseManager
{
    List<string> personTearDown = new List<string>();
    List<string> paymentTearDown = new List<string>();
    List<string> accountTearDown = new List<string>();

	public TestDatabaseManager()
	{
		
	}

    public bool connect()
    {
        return DatabaseManager.connectLocal();
    }

    public bool testGetPerson(){
        bool ok = true;

        foreach (string s in personTearDown)
            ok = ok & (DatabaseManager.getPerson(s) != null);
        
        return ok;
    }

    public bool testSavePerson(){
        bool ok = false;
       
        // ulozi
        Person p1 = new Person("com", Configuration.PersonStatus.active.ToString("g"), null, 
                              null, null, null, null, null, null, accountTearDown[0], "asfdcx");

        // neulozi
        Person p2 = new Person("cz", Configuration.PersonStatus.blocked.ToString("g"), null, 
                              null, null, null, null, null, null, null, null);

        // neulozi stejne cisla uctu
        Person p3 = new Person("ger", Configuration.PersonStatus.active.ToString("g"), "test", 
                              "test", "test", "test", "test", "test", "test", "10007", "asfdcx");

        ok = DatabaseManager.savePerson(ref p1);
        if (ok) 
            personTearDown.Add(p1.Email);

        if (DatabaseManager.savePerson(ref p2))
        {
            personTearDown.Add(p2.Email);
            ok = ok & false;
        }   
        else
            ok = ok & true;

        if (DatabaseManager.savePerson(ref p3))
        {
            personTearDown.Add(p2.Email);
            ok = ok & false;
        }
        else
            ok = ok & true;

        return ok;
    }

    public bool testEditPerson(){
        bool ok = false;

        // upravi
        Person p1 = new Person("com", Configuration.PersonStatus.active.ToString("g"), null,
                              null, null, null, null, null, null, accountTearDown[0], "testGUID");

        // neupravi conflicted fk
        Person p2 = new Person("ge", Configuration.PersonStatus.active.ToString("g"), "test", 
                              "test", "test", "test", "test", "test", "test", "10001", "1111111");

        ok = DatabaseManager.editPerson(p1.Email, ref p1);
        if (ok) 
            personTearDown[0] = p1.Email;

        if (DatabaseManager.editPerson(p2.Email, ref p2))
        {
            personTearDown[1] = p2.Email;
            ok = ok & false;
        }
        else
            ok = ok & true;

        return ok;
    }

    public bool testRemovePerson()
    {
        bool ok = true;

        foreach (string s in personTearDown)
            ok = ok & DatabaseManager.removePerson(s);
        return ok;
    }

    // ____________________________________
    public bool testGetAccount()
    {
        return (DatabaseManager.getAccount("10001") != null && DatabaseManager.getAccount("a") == null);
    }

    public bool testSaveAccount()
    {
        Account ac = new Account();
        bool ok = false;

        ac.Balance = "1000";
        ac.CreationDate = DateTime.Now.ToShortDateString();
        ac.IncomesCount = "40";
        ac.OutcomesCount = "55";

        ok = DatabaseManager.saveAccount(ref ac);
        
        if (ok)
        {
            accountTearDown.Add(ac.Id);
        }

        string currentId = ac.Id;

        ac.Balance = null;
        ac.IncomesCount = null;
        ac.OutcomesCount = "-4";

        if (DatabaseManager.saveAccount(ref ac))
        {
            accountTearDown.Add(ac.Id);
            ok = ok & false;
        }
        else
            ok = ok & true;


        return ok;
    }

    public bool testEditAccount()
    {
        Account ac = new Account("", "7000", "", "7", "6");

        if (DatabaseManager.editAccount(accountTearDown[0], ref ac))
        {
            accountTearDown[0] = ac.Id;
            return true;
        }
        return false;
    }

    public bool testRemoveAccount()
    {
        bool ok = true;
        foreach (string s in accountTearDown)
        {
            ok = ok & DatabaseManager.removeAccount(s);
        }
        return ok;
    }


    // ____________________________________
    public bool testGetPayment()
    {
        return (DatabaseManager.getPayment("1") != null && DatabaseManager.getPayment("a") == null);
    }

    public bool testSavePayment()
    {
        bool ok = false;
        Payment p = new Payment("", "848fbrf", "10001", "93yh", "owejhfow", "dweudhwe", "nic", "edj", "212", "r23r", "efwer");

        ok = DatabaseManager.savePayment(ref p);

        p = new Payment("", "", "", "", "", "", "", "", "", "", "");

        ok = ok && !DatabaseManager.savePayment(ref p);

        return ok;

    }
    public bool testEditPayment()
    {
        Payment p = new Payment("", "kjnnkjn", "10001", "td", "owejhfow", "dweudhwe", "nic", "edj", "100", "r23r", "nocomment");
        return DatabaseManager.editPayment("1", ref p);
    }

}
